enyo.kind({
	name: "enyo.AssessmentReportRun",
	kind: enyo.PanelBase,
	runPageSize: 5,
	runPageNumber: 1,
	runPageCount: 1,
	runList: null,
	
	currentLoading: null,
	currentReportList: null,
	currentReportPayload: null,
	
	components: [
		{kind: "PanelContent", headerContent: "Assessment Run List", onSignOut: "doSignOut", onBack: "back", flex: 1, components: [
			{className: "margin-medium", components: [
				{kind: "Group", caption: "Please Tab the Report to View", components:[
					{kind: "VirtualRepeater", name: "runRepeater", onSetupRow: "runRepeaterSetupRow", components: [
						{kind: "Item", layoutKind: "HFlexLayout", /*onclick: "listItemClick",*/ style: "border: none", components: [
							{name: "runInfo", flex: 1, kind : "DividerDrawer", caption : "", open: false, onOpenChanged: "openChangedOpenChanged", components : [
								{name: "runIDContent", content : "", style: "display: none"},
								{name: "statusCodeContent", content : "", style: "display: none"},
								{name: "runInfoContent", content: "", style: "padding: 0px 10px 10px 20px; font-weight: bold;", flex: 1},
								{name: "loadingContent", className: "divider-content"},
								{kind: "VirtualRepeater", name: "reportRepeater", onSetupRow: "reportRepeaterSetupRow", style: "padding: 0px 10px 0px 10px", components: [
									{kind: "Item", layoutKind: "HFlexLayout", className: "func-desc", style: "padding: 10px;", onclick: "itemClick", components: [
										{name: "reportIDContent", content: "" ,  style: "display: none"},
										{name: "reportInfoContent", content: "" , flex: 1}
									]}
								]}
							]},
							{name: "moreButton", content : "Tap Here to Load More...", style: "margin: 5px 0px 5px 10px; font-weight: bold; font-style: italic;", onclick: "moreButtonClicked", showing: false}, 
						]}
					]}
				]}
			]}
		]},
		{name: "runListService", kind: "WebService", method: "POST", onSuccess: "runListServiceGot", onFailure: "runListServiceFailure"},
		{name: "reportListService", kind: "WebService", method: "POST", onSuccess: "reportListServiceGot", onFailure: "reportListServiceFailure"}
	],
	
	create: function() {
		this.inherited(arguments);
		this.initPanel();
	},
	
	initPanel: function()
	{
		this.showScrim();
		
		//reset the controls display and status
		enyo.Data.selectedReportID = -1;
		
		//call the service
		var obj = new Object;
		obj.value0 = "mark.davis2@hp.com";
		obj.value1 = this.runPageSize;
		obj.value2 = this.runPageNumber;
		this.$.runListService.setUrl(enyo.Config.getServiceUrl_RetrieveRunList());
		this.$.runListService.setHeaders(enyo.Config.getCommonServiceHeader());
		this.$.runListService.call(obj);
	},
	
	back: function() {
		this.parentWnd.switchView("home");
	},
	
	runListServiceGot: function(inSender, inResponse, inRequest) {
		if(inResponse.statusCode === 200) {
			if(this.runList == null) {
				this.runList = new Array();
			}
			
			if(inResponse.payload.analysisRunSBSList == null) {
				this.doError("No item found");
			} else {
				for(var i = 0; i< inResponse.payload.analysisRunSBSList.length; i++) {
					this.runList.push(inResponse.payload.analysisRunSBSList[i]);
				}
			}
			
			//store the page count
			this.runPageCount = inResponse.payload.pageCount;
			
			//show device list
			this.$.runRepeater.render();
			
			this.hideScrim();	//finally, initialize work done, chagne the UI
		}
		else {
			this.doError("Error response of service (StatusCode: " + inResponse.statusCode + ")");
		}
	},
	
	gotRunFailure: function(inSender, inResponse, inRequest) {
		this.doError("Failed calling service");
	},
	
	runRepeaterSetupRow: function(inSender, inIndex)
	{
		if(this.runList == null) return;	//device list not loaded
		if (inIndex < this.runList.length) {
			//var isRowSelected = (inIndex == this.selectedDeviceIndex);
			//this.$.item.applyStyle("background", isRowSelected ? "#000" : null);
			this.$.runInfo.setCaption(" [Run#" + this.runList[inIndex].id + " Request#" + this.runList[inIndex].analysisRequestId + "] " + this.runList[inIndex].type + "; " + this.runList[inIndex].statusCode);
			this.$.runIDContent.setContent(this.runList[inIndex].id);
			this.$.statusCodeContent.setContent(this.runList[inIndex].statusCode);
			this.$.runInfoContent.setContent(this.runList[inIndex].statusDefaultDescription);
			
			this.$.moreButton.setShowing(false);
			this.$.runInfo.setShowing(true);
		
			return true;
		}
		else if(inIndex == this.runList.length && this.runPageNumber < this.runPageCount && inIndex != 0) {
			this.$.moreButton.setShowing(true);
			this.$.runInfo.setShowing(false);
			return true;
		}
	},
	
	moreButtonClicked: function() {
		this.showScrim();
		
		this.runPageNumber ++;
		
		//call the service
		var obj = new Object;
		obj.value0 = "mark.davis2@hp.com";
		obj.value1 = this.runPageSize;
		obj.value2 = this.runPageNumber;
		this.$.runListService.setUrl(enyo.Config.getServiceUrl_RetrieveRunList());
		this.$.runListService.setHeaders(enyo.Config.getCommonServiceHeader());
		this.$.runListService.call(obj);
	},
	
	openChangedOpenChanged: function(inSender, inEvent) {
		if(inSender.open) {
			var statusCode = this.$.statusCodeContent.content;
			if(statusCode == "COMPLETED")
			{
				this.currentLoading = this.$.loadingContent;
				this.currentReportList = this.$.reportRepeater;
				this.currentLoading.setContent("Loading...");
				
				this.showScrim();
				
				//call the service
				var obj = new Object;
				obj.value0 = this.$.runIDContent.content;
				this.$.reportListService.setUrl(enyo.Config.getServiceUrl_RetrieveReportList());
				this.$.reportListService.setHeaders(enyo.Config.getCommonServiceHeader());
				this.$.reportListService.call(obj);
			}
		}
	},
	
	reportListServiceGot: function(inSender, inResponse, inRequest) {
		if(inResponse.statusCode === 200) {
			if(inResponse.payload.length <= 0) {
				this.doError("No item found");
			} else {
				this.currentReportPayload = inResponse.payload;
			}
			
			//show list
			this.currentReportList.render();
			this.currentLoading.setContent("");
			
			this.currentReportList = null;
			this.currentLoading = null;
			this.currentReportPayload = null;
			
			this.hideScrim();	//finally, initialize work done, chagne the UI
		}
		else {
			this.doError("Error response of service (StatusCode: " + inResponse.statusCode + ")");
		}
	},
	
	gotReportFailure: function(inSender, inResponse, inRequest) {
		this.doError("Failed calling service");
	},
	
	reportRepeaterSetupRow: function(inSender, inIndex) {
		if(this.currentReportPayload == null) return;
		if(inIndex < this.currentReportPayload.length) {
			//return {kind: "Item", layoutKind: "HFlexLayout", onclick: "itemClick", components: [
			//	{content: "Report ID:" + this.currentReportPayload[inIndex].guid , flex: 1}
			//]};
			this.$.reportIDContent.setContent(this.currentReportPayload[inIndex].saDocKey);
			this.$.reportInfoContent.setContent(this.currentReportPayload[inIndex].name + " [" + this.currentReportPayload[inIndex].language + "]");
			return true;
		}
	},
	
	itemClick: function(inSender)
	{
		enyo.Data.selectedReportID = this.$.reportIDContent.content;
		this.parentWnd.switchView("report_detail");
	}
});